import Vue from "vue";
import Vuex from "vuex";
import { storage } from "@/plugins/storage";
import * as types from "./mutation-types";
import { fastLogin } from "../apis";
import { getSSCApplyInfo, getSSCSign } from "@/apis/test";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    access_token: "",
    // menus: []
    cards: [],
    user: {}
  },
  mutations: {
    [types.SET_ACCESS_TOKEN](state, session) {
      storage.set(
        types.ACCESS_TOKEN,
        session.access_token,
        parseInt(session.expires_in) * 1000
      );
      state.access_token = session.access_token;
    },
    [types.REMOVE_ALL](state) {
      state.access_token = "";
      storage.clear();
    },
    [types.CARDS](state, obj) {
      storage.set(types.CARDS, obj);
      state.cards = obj;
    },
    [types.USER](state, obj) {
      storage.set(types.USER, obj);
      state.user = obj;
    }
  },
  getters: {},
  actions: {
    [types.CARDS]({ commit }, obj) {
      commit(types.CARDS, obj);
    },
    [types.USER]({ commit }, obj) {
      commit(types.USER, obj);
    },
    login({ commit }, params) {
      return new Promise((resolve, reject) => {
        if (params.access_token && params.expires_in) {
          commit(types.SET_ACCESS_TOKEN, params);
          resolve(params.access_token);
        } else if (params.username && params.password) {
          fastLogin(params)
            .then(res => {
              commit(types.SET_ACCESS_TOKEN, res);
              resolve(res.access_token);
            })
            .catch(err => {
              reject(err);
            });
        } else {
          reject(new Error("miss params"));
        }
      });
    },
    logout({ commit }) {
      commit(types.REMOVE_ALL);
    },
    getSSCTestSign({ commit }, params) {
      // http://172.30.3.217:10112/swagger-ui.html
      // 测试时，获取签名
      return new Promise((resolve, reject) => {
        getSSCApplyInfo().then(sscApplyRes => {
          getSSCSign({
            channelNo: sscApplyRes.data.channelNo,
            signNo: sscApplyRes.data.signNo,
            // signNo: params.signNo,
            openId: sscApplyRes.openId || params.openId,
            isIndep: params.isIndep,
            isWeb: "1",
            // isChannelScan: "1",
            // historyFlag: "",
            isAuthFace: "",
            // qrCode: "",
            // treatmentAuth: "",
            returnUrl: params.uri
          }).then(signRes => {
            if (signRes.ok) {
              resolve({
                sign: signRes.data,
                signNo: sscApplyRes.data.signNo
              });
            } else {
              reject(new Error("get sign fail"));
            }
          });
        });
      });
    }
    // getSSCTestSign({ commit }, params) {
    //   // http://172.30.3.217:10112/swagger-ui.html
    //   // 测试时，获取签名
    //   return new Promise((resolve, reject) => {
    //     fastLogin({
    //       username: params.aac002,
    //       password: params.ggfwPassword
    //     })
    //       .then(loginRes => {
    //         commit(types.SET_ACCESS_TOKEN, loginRes);
    //         getSSCApplyInfo({
    //           channelNo: params.channelNo
    //         })
    //           .then(sscApplyRes => {
    //             getSSCSign({
    //               aac002: sscApplyRes.idcard,
    //               aac003: sscApplyRes.userName,
    //               aab301: sscApplyRes.aab301,
    //               aac067: sscApplyRes.aac067,
    //               channelNo: sscApplyRes.channelNo,
    //               signNo: sscApplyRes.signNo,
    //               openId: sscApplyRes.openId || params.openId,
    //               isIndep: "1",
    //               historyFlag: "",
    //               isAuthFace: "",
    //               qrCode: "",
    //               treatmentAuth: ""
    //             })
    //               .then(signRes => {
    //                 if (signRes.msgCode === "200") {
    //                   resolve({
    //                     sign: signRes.result,
    //                     signNo: sscApplyRes.signNo
    //                   });
    //                 } else {
    //                   reject(new Error("get sign fail"));
    //                 }
    //               })
    //               .catch(err => {
    //                 reject(err);
    //               });
    //           })
    //           .catch(err => {
    //             reject(err);
    //           });
    //       })
    //       .catch(err => {
    //         reject(err);
    //       });
    //   });
    // }
  },
  modules: {}
});
